<!DOCTYPE html>
<html>
    <head>
		<meta charset="utf-8"/>
		<meta name="renderer" content="webkit">
		<meta http-equiv="Cache-Control" content="no-siteapp" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    	<title>新增商品</title>
    	<meta name="keywords" content="商品 管理员" />
    	<meta name="description" content="新增商品" />
		<meta name = "format-detection" content = "telephone=no">
    	<link rel="stylesheet" href="" type="text/css" />
		<link rel="shortcut icon" href="favicon.ico" /> 
		<?php
			session_start();
			 error_reporting(0);
			require('inc/func.php');
			require('inc/mysql.php');
			if($_GET['out']==true){
				session_destroy();
				redirect('','redirect','login/index.php');
				exit();
			}
			if(empty($_SESSION['loginUserName'])){redirect('','redirect','login/index.php');}
			$db->query('select * from adminUser where username=\''.$_SESSION['loginUserName'].'\' and password=\''.$_SESSION['loginPassword'].'\' limit 1');
				if($db->num_rows()<0){redirect('非法访问请登录！','redirect','login/index.php');exit();}
		    $adminUser=$db->query_first('select * from adminUser where id=1 limit 1');
		    $adminUsername=$adminUser['username'];
			
			if($_GET['action']=='save'){
				$num=$_POST['num'];
				$delete=$_POST['delete'];
				$good_arr[0]=array(
					'attr_list'=>array(
					         0=>array(
							   "attr_id"=>"",
							   "attr_name"=>""
							)
					       ),
					'num'=>$num,
					"price"=>'',
					"no"=>"",
					"pic"=>""
                );
				$good_attr=json_encode($good_arr);
				$imgname2=$_POST['imgname2'];
				$goods_group=$_POST['goods_group'];
				$goods_name=$_POST['goods_name'];
				$goods_price=$_POST['goods_price'];
				$goods_number=$_POST['goods_number'];
				//$goods_cun=$_POST['goods_cun'];
				$goods_detail=$_POST['goods_detail'];
				$imgname=$_POST['imgname'];
				$imgnameHouse=$_POST['imgnameHouse'];
				$goods_group=!empty($goods_group)?$goods_group:redirect('未选择分类','redirect','index.php');
				$goods_name=!empty($goods_name)?$goods_name:redirect('名字不能为空','redirect','index.php');
				if($goods_price<=0){redirect('价格不能为空','redirect','index.php');}
				$goods_number=!empty($goods_number)?$goods_number:redirect('货号不能为空','redirect','index.php');
				$goods_detail=!empty($goods_detail)?$goods_detail:redirect('商品详情不能为空','redirect','index.php');
				$db->query('insert into hjmall_goods set store_id=2,name=\''.$goods_name.'\',price='.$goods_price.',detail=\''.$goods_detail.'\',cover_pic=\''.$imgname.'\',video_url=\''.$imgnameHouse.'\',cat_id='.$goods_group.',sort=1000,addtime='.time().',attr=\''.$good_attr.'\',status='.$delete);
			    $good_id=$db->insert_id();
				if(!empty($imgname2)){
					$imgPic=explode('|',$imgname2);
					foreach($imgPic as $key=>$val){
						if(!empty($val)){
							$db->query('insert into hjmall_goods_pic set goods_id='.$good_id.',pic_url=\''.$val.'\'');
						}
					}
				}
				redirect('添加成功','redirect','index.php');
			}
		?>
		<style type="text/css">
			.formDiv .txtInput {width:55%;font-size:16px;height:20px;padding:10px 10px;border:1px solid #e4e4e4;}
			.submitBtn{display:block;width:50%;height:40px;line-height:40px;border-radius:3px;margin:0 auto;background:#189ad8;color:#fff;font-size:16px;cursor:pointer;}
			.formDiv .add{font-size:14px;}

				.file {
					font-size:14px;
					color:#189ad8;
				}
				.file input {
					position: absolute;
					top:15px;right:-35px;
					opacity: 0;
				}
				.file:hover {
					background: #AADFFD;
					border-color: #78C3F3;
					color: #004974;
					text-decoration: none;
				}
			#feedback img,#feedback2 img,#feedback3 img{
				border: 1px solid silver;
				border-radius:3px;
				padding: 3px; 
				width: 80px;height:80px;
			}
			#feedback,#feedback2,#feedback3{position:relative;margin-top:15px;text-align:center;background:#fff;}
			#feedback .imgClose,#feedback2 .imgClose,#feedback3 .imgClose{width:20px;height:20px;position:absolute;right:-15px;top:-10px;cursor:pointer;padding:0px;border:0px;border-radius:0px;}
			#feedback .appendP,#feedback2 .appendP{width:80px;height:65px;position:relative;margin:auto;margin-top:5px;margin-bottom:20px;}
			#feedback3 .appendP{width:80px;height:65px;position:relative;margin-top:20px;margin-bottom:20px;float:left;margin-left:20px;}
			.houseAdmin .right{width:100%;}
			.houseAdmin .right .top{height:55px;}
			.houseAdmin .right .top .caidan{margin-top:10px;float:left;width:32px;height:32px;margin-left:20px;cursor:pointer;}
			.houseAdmin .right .top .input{border:0px;width:160px;float:left;height:30px;}
			.houseAdmin .right .top .touxiang{width:32px;height:32px;float:right;margin-top:10px;margin-right:10px;}
			.houseAdmin .right .top .touxiang img{max-width:100%;}
			.houseAdmin .right .top .xiala{width:12px;height:12px;float:right;margin-right:10px;margin-top:25px;cursor:pointer;position:relative;}
			.houseAdmin .right .top .xiala .xialaContent{width:110px;text-align:right;border:1px solid #ccc;position:absolute;top:-25px;right:-10px;padding-top:55px;display:none;padding-bottom:1px;}
			.houseAdmin .right .top .xiala .xialaContent a{font-size:12px;display:inline-block;width:100%;height:25px;line-height:25px;background:#3bbfb4;color:#fff;text-decoration:none;text-align:center;border-bottom:1px solid #fff;}
			.houseAdmin .right .top .xiala img{max-width:100%;}
			.houseAdmin .right .top .text{float:right;display:inline-block;margin-top:25px;margin-right:10px;}
		</style>
    </head>
    <body>
		<div class="houseAdmin" id="houseAdmin">
			<div class="right">
				<div class="top">
					<div class="caidan"><a href=""><img src="inc/images/caidan.png" alt=""/></a></div>
					
					<div class="xiala">
						<img src="inc/images/xiala.png" alt=""/>
						<div class="xialaContent">
							<a href="?out=true">loginOut</a>
							<?php if($_SESSION['loginUserName']==$adminUsername){?>
							<a href="addAdmin.php">添加管理员</a>
							<a href="editAdmin.php">修改账号和密码</a>
							<?php }?>
						</div>
					</div>
					<span class="text"><?php echo $_SESSION['loginUserName'];?></span>
					<div class="touxiang">
						<img src="inc/images/touxiang.png" alt=""/>
					</div>
				</div>	
			</div>
		</div>
		<form action="?action=save" method="post" enctype="multipart/form-data"> 
			<div class="formDiv">
				<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">商品分类：</span>
				<select  name="goods_group" id="goods_group">
					<option value="0">请选择</option>
				    <?php 
						$db->query('select * from hjmall_cat where parent_id=0');
						while($row=$db->next_record()){
						$id_arr[]=$row;
						}
						foreach($id_arr as $val){
						   $db->query('select * from hjmall_cat where parent_id='.$val['id']);
						   while($db->next_record()){
					?>
							<option value="<?php echo $db->f('id');?>"><?php echo $val['name'].'-'.$db->f('name');?></option>
						   <?php }?>
					<?php }?>
				</select>
				<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">商品名称：</span><input type="text" name="goods_name" id="goods_name" class="txtInput" value="" /></p>
				<!--<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">库存：</span><input type="text" name="goods_cun" id="goods_cun" class="txtInput" value="" /></p>-->
				<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">货号：</span><input type="text" name="goods_number" id="goods_number" class="txtInput" value="" /></p>
				<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">售价：</span><input type="text" name="goods_price" id="goods_price" class="txtInput" value="" /></p>
				<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">库存：</span><input type="text" name="num" id="num" class="txtInput" value="" /></p>
				<p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">详情介绍：</span><textarea  name="goods_detail" id="goods_detail" class="txtInput"/></textarea></p>
			    <p class="mb15"><span style="display:inline-block;width:30%;padding-left:15px;">上架：</span>
				是:<input type="radio" name="delete" value="1" checked/>&nbsp;
				否:<input type="radio" name="delete" value="0" />
				</p>
			</div>
			<div class="pCenter" id="feedback">
				    <span style="position:absolute;top:15px;left:10px;">商品缩略图</span>
					<a href="javascript:;" class="file" style="display:inline-block;width:50px;height:50px;background:url(xj.png) no-repeat center center;border:1px dashed;">
						<input type="file" name="imgfile" id="imgfile">
					</a><br/>
					<input type="text" name="imgname" id="imgname" hidden/>
			</div> 
			<div style="clear:both;"></div>
			<div class="pCenter" id="feedback2">
				    <span style="position:absolute;top:15px;left:10px;">商品视频</span>
					<a href="javascript:;" class="file" style="display:inline-block;width:50px;height:50px;background:url(sp.png) no-repeat center center;border:1px dashed;">
						<input type="file" name="imgHouse" id="imgHouse">						
					</a><br/>
					<input type="text" name="imgnameHouse" id="imgnameHouse" hidden/>
			</div> 
			<div style="clear:both;"></div>
			<div class="pCenter" id="feedback3">
				    <span style="position:absolute;top:15px;left:10px;">商品图片</span>
					<a href="javascript:;" class="file" style="display:inline-block;width:50px;height:50px;background:url(jia.png) no-repeat center center;border:1px dashed;">
						<input type="file" name="imgfile2" id="imgfile2">
					</a><br/>
					<input type="text" name="imgname2" id="imgname2" hidden/>
			</div> 
			<div style="clear:both;"></div>
			<p class="pCenter mt15"><input type="submit" value="保存" class="submitBtn" /></p>
		</form>
		<div style="height:20px;clear:both;overflow:hidden;"></div>
		<script type="text/javascript" src="inc/jquery-1.7.js"></script>
		<script type="text/javascript">
		  
			$(document).ready(function(){
				$('#houseAdmin').find('.xiala').click(function(){
					$(this).find('.xialaContent').css('display','block');
				})
				$("#imgfile").change(function(){
					var data = new FormData();
					$.each($('#imgfile')[0].files, function(i, file) {
						data.append('upload_file'+i, file);
					});
					$.ajax({
						url:'up.php',
						type:'POST', 
						data:data,
						cache: false,
						contentType: false,
						processData: false,
						success:function(data){
							if(data!=''){
								$('#clear').remove();
								$("#feedback").append('<p class="appendP"><img class="phpimg" src='+data+' title=""/><img src="inc/images/close.png" alt="" class="imgClose"></p>');
								$("#feedback").find('.imgClose').css('visibility','visible');
								$('#imgname').val('https://wx.too8tu.top/admin/'+data);
								$('#feedback').find('.file').hide()
								$('#feedback').find('.imgClose').click(function(){
									$(this).parent('.appendP').remove();
									$('#feedback').find('.file').show()
								})
							}
						},
						error:function(){
							alert('上传出错');
						}
					});
				});
				
				$("#imgfile2").change(function(){
					var data = new FormData();
					$.each($('#imgfile2')[0].files, function(i, file) {
						data.append('upload_file'+i, file);
					});
					$.ajax({
						url:'up.php',
						type:'POST', 
						data:data,
						cache: false,
						contentType: false,
						processData: false,
						success:function(data){
							if(data!=''){
								$('#clear').remove();
								$("#feedback3").append('<p class="appendP"><img class="phpimg" src='+data+' title=""/><img src="inc/images/close.png" alt="" class="imgClose"></p><div style="clear:both" id="clear"></div>');
								$("#feedback3").find('.imgClose').css('visibility','visible');
								$('#imgname2').val($('#imgname2').val()+'https://wx.too8tu.top/admin/'+data+'|');
								$('#feedback3').find('.imgClose').click(function(){
									$(this).parent('.appendP').remove();
									$src=$(this).prev('.phpimg')[0].src+'|';
									$('#imgname2').val($('#imgname2').val().replace($src,''));
								})
							}
						},
						error:function(){
							alert('上传出错');
						}
					});
				});
				
				
				$("#imgHouse").change(function(){
					var data = new FormData();
					$.each($('#imgHouse')[0].files, function(i, file) {
						data.append('upload_file'+i, file);
					});
					$.ajax({
						url:'upm.php',
						type:'POST', 
						data:data,
						cache: false,
						contentType: false,
						processData: false,
						success:function(data){
							console.log(data)
							if(data!=''){
								$('#clear2').remove();
								$("#feedback2").append('<p class="appendP" style="width:120px;"><img class="phpimg" src='+data+' title="视频已上传" alt="视频已上传"/><img src="inc/images/close.png" alt="" class="imgClose"></p><div style="clear:both" id="clear2"></div>');
								$("#feedback2").find('.imgClose').css('visibility','visible');
								$('#imgnameHouse').val('https://wx.too8tu.top/admin/'+data);
								$('#feedback2').find('.file').hide()
								$('#feedback2').find('.imgClose').click(function(){
									$(this).parent('.appendP').remove();
									$('#feedback2').find('.file').show()
								})
							}
						},
						error:function(){
							alert('上传出错');
						}
					});
				});
				
			});
		</script>
	</body>
</html>